package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.example.myapplication.domain.ClassAndTagItem;
import com.example.myapplication.domain.Student;
import com.example.myapplication.util.MyAdapter;
import com.google.gson.Gson;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private OkHttpClient okHttpClient;
    private ClassAndTagItem classAndTagItem;

    private List<ClassAndTagItem.DataDTO.ClassDTO> classXs;

    private List<ClassAndTagItem.DataDTO.ClassDTO.ContentCollectDTO> contentCollect;

    private RecyclerView rs_class;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        okHttpClient = new OkHttpClient();
        rs_class = findViewById(R.id.rs_class);
        //请求接口获取数据
        this.getSnClassAndTag();
    }

    private void getSnClassAndTag() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String url = "http://175.178.156.135:1690/Sn/GetSnClassAndTag?sn=CRS2A3B00001";
                    Request request = new Request.Builder().url(url).build();
                    Call call = okHttpClient.newCall(request);
                    Response response = call.execute();    //同步请求,阻塞线程
                    if (response.code() == HttpURLConnection.HTTP_OK) {
                        Gson gson = new Gson();
                        classAndTagItem = gson.fromJson(response.body().string(), ClassAndTagItem.class);
                        classXs = classAndTagItem.getData().getClassX();
                        handler.sendEmptyMessage(1);
                    }
                } catch (Exception e) {
                    Log.d("解析失败", e.toString());
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static void setDrawableLeft(Context context, String imageUrl, TextView tw) {
        RequestOptions requestOptions = new RequestOptions().centerCrop();
        Glide.with(context)
                .load(imageUrl)
                .apply(requestOptions)
                .into(new CustomTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable com.bumptech.glide.request.transition.Transition<? super Drawable> transition) {
                        tw.setCompoundDrawablesWithIntrinsicBounds(resource, null, null, null);

                    }
                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                        // 当图像加载被清除时的处理
                    }
                });
    }

    private  Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            //设置适配器
            rs_class.setLayoutManager(new LinearLayoutManager(MainActivity.this));
            MyAdapter adapter = new MyAdapter(MainActivity.this, classXs);
            rs_class.setAdapter(adapter);
        }

    };



}